<?php
header("Access-Control-Allow-Origin:*");

/**
 * 首页置顶文章
 * @param unknown_type $pars
 * @throws Exception
 */
function toparticle($pars){
	$db =&db();
	$status=0;
	$list=array();
	try{
		if(empty($pars->uid)){
			throw new Exception('当前人员不存在');
		}
		$sql="select article_id,title,file_url,
CONCAT('".SITE_URL."','/index.php?app=article&act=view&article_id=',article_id) as url
		from cw_article a LEFT JOIN cw_acategory ac on a.cate_id=ac.cate_id where ac.cate_name='首页置顶'
		ORDER BY a.sort_order,article_id desc";
		$list=$db->getAll($sql);

		$status=1;
	}
	catch (Exception $exp){
		$list=$exp->getMessage();
	}
	return JSONResult($status,$list,$pars);
}

/**
 * 首页推荐活动
 * @param unknown_type $pars
 * @throws Exception
 */
function recactivity($pars){
	$db =&db();
	$status=0;
	$list=array();
	try{
		if(empty($pars->uid)){
			throw new Exception('当前人员不存在');
		}
		$sql="select act_id,title,cat,pic from cw_activity ORDER BY sort_order,act_id desc limit 0,4";
		$list=$db->getAll($sql);

		$status=1;
	}
	catch (Exception $exp){
		$list=$exp->getMessage();
	}
	return JSONResult($status,$list,$pars);
}

/**
 * 首页微博
 * @param unknown_type $pars
 * @throws Exception
 */
function weibolist($pars){
	$db =&db();
	$status=0;
	$list=array();
	try{
		$row=empty($pars->row)?0:$pars->row;
		$size=empty($pars->size)?10:$pars->size;
		if(empty($pars->uid)){
			throw new Exception('当前人员不存在');
		}
		$sqlwhere = ' 1=1';
		$sqlorder = 'wei_id desc';
		if(property_exists($pars,'type')){
			if($pars->type=='hot'){
				$sqlwhere.=' and w.uid<>'.$pars->uid;
				$sqlorder='replaynum desc';
			}
			else if($pars->type=='collect'){
				$sqlwhere.=" and exists(select 1 from cw_follow where type='weibo' and outkey=wei_id and uid=".$pars->uid.")";
			}
			else if($pars->type=='self'){
				$sqlwhere.=' and w.uid='.$pars->uid;
			}
			else if($pars->type=='follow'){
				$sqlwhere.=" and exists(select 1 from cw_follow where type='user' and outkey=w.uid and cw_follow.uid=".$pars->uid.")";
			}
			else{
				//$sqlwhere.=' and w.uid<>'.$pars->uid;
			}
		}
		$sql="select wei_id,user_id,user_name,case when ifnull(real_name,'')<>'' then real_name else user_name end as real_name,
		headimgurl,title,w.pic,sharenum,replaynum,follownum,praisenum,w.device,f.follow_id,f1.follow_id as ufollow_id,praise_id,pubdate,content,file_path,file_pic,wei_pic,video_id ,
		ifnull(pet.`name`,'') as petname,TIMESTAMPDIFF(year, ifnull(pet.age,curdate()), curdate()) as petage,ifnull(pet.avatar,'') as petavatar,ifnull(pet.cate,'') as petcate ,ifnull(pet.sex,'') as petsex
		from cw_weibo w
		LEFT JOIN cw_follow f on w.wei_id=f.outkey and f.type='weibo' and f.uid=".$pars->uid." 
		LEFT JOIN cw_follow f1 on w.uid=f1.outkey and f1.type='user' and f1.uid=".$pars->uid." 
		LEFT JOIN cw_praise p on w.wei_id=p.outkey and p.type='weibo' and p.uid=".$pars->uid." 
		LEFT JOIN cw_member m on w.uid=m.user_id
		LEFT JOIN cw_pet pet on w.uid=pet.uid and IFNULL(isDefault,0)=1
		where $sqlwhere 
		order by $sqlorder limit $row,$size";
		$wlist=$db->getAll($sql);
		foreach ($wlist as $k=>$v){
			$v['pubdate']=time_tran(local_date('Y-m-d H:i:s', $v['pubdate']));
			$list[]=$v;
		}
		$status=1;
	}
	catch (Exception $exp){
		$list=$exp->getMessage();
	}
	return JSONResult($status,$list,$pars);
}

/**
 * 关注
 * @param unknown_type $pars
 */
function follow($pars){
	$db =&db();
	$status=0;
	$list=array();
	try{
		$outkey=empty($pars->outkey)?0:$pars->outkey;
		$type=empty($pars->type)?'':$pars->type;
		if(empty($pars->uid)){
			throw new Exception('当前人员不存在');
		}

		if(empty($outkey)){
			throw new Exception('关注失败');
		}
		if(empty($type)){
			throw new Exception('关注失败');
		}

		$sql="select 1 from cw_follow where type='".$type."' and outkey='".$outkey."' and uid=".$pars->uid;
		$exsits=$db->getOne($sql);
		if($exsits){
			throw new Exception('您已经关注过了');
		}

		$sql="insert into cw_follow set type='".$type."', outkey='".$outkey."',uid=".$pars->uid." ,fodate='".gmtime()."'";
		$db->query($sql);
		if(!$db->affected_rows()){
			throw new Exception('关注失败');
		}
		if($type=='weibo'){
			$sql="update cw_weibo left join (select count(1) as c,outkey from cw_follow where type='".$type."' and outkey='".$outkey."') x
			on wei_id=x.outkey set follownum=x.c where wei_id=".$outkey;
			$db->query($sql);
		}
		if($type=='goods'){
			$sql="update cw_goods_statistics left join (select count(1) as c,outkey from cw_follow where type='".$type."' and outkey='".$outkey."') x
			on goods_id=x.outkey set collects=x.c where goods_id=".$outkey;
			$db->query($sql);
		}
		if($type=='video'){
			$sql="update cw_video left join (select count(1) as c,outkey from cw_follow where type='".$type."' and outkey='".$outkey."') x
			on video_id=x.outkey set follow_num=x.c where video_id=".$outkey;
			$db->query($sql);
		}
		if($type=='article'){
			$sql="update cw_article left join (select count(1) as c,outkey from cw_follow where type='".$type."' and outkey='".$outkey."') x
			on article_id=x.outkey set follow_num=x.c where article_id=".$outkey;
			$db->query($sql);
		}
		if($type=='user'){

		}
		$list='关注成功';

		$status=1;
	}
	catch (Exception $exp){
		$list=$exp->getMessage();
	}
	return JSONResult($status,$list,$pars);
}

/**
 * 取消关注
 * @param unknown_type $pars
 */
function unfollow($pars){
	$db =&db();
	$status=0;
	$list=array();
	try{
		$outkey=empty($pars->outkey)?0:$pars->outkey;
		$type=empty($pars->type)?'':$pars->type;
		if(empty($pars->uid)){
			throw new Exception('当前人员不存在');
		}

		if(empty($outkey)){
			throw new Exception('取消关注失败');
		}
		if(empty($type)){
			throw new Exception('取消关注失败');
		}

		$sql="select follow_id from cw_follow where type='".$type."' and outkey='".$outkey."' and uid=".$pars->uid;
		$follow_id=$db->getOne($sql);
		if(!$follow_id){
			throw new Exception('您没有关注过');
		}

		$sql="delete from cw_follow where follow_id=".$follow_id;
		$db->query($sql);
		if(!$db->affected_rows()){
			throw new Exception('取消关注失败');
		}
		if($type=='weibo'){
			$sql="update cw_weibo left join (select count(1) as c,outkey from cw_follow where type='".$type."' and outkey='".$outkey."') x
			on wei_id=x.outkey set follownum=ifnull(x.c,0) where wei_id=".$outkey;
			$db->query($sql);
		}
		if($type=='goods'){
			$sql="update cw_goods_statistics left join (select count(1) as c,outkey from cw_follow where type='".$type."' and outkey='".$outkey."') x
			on goods_id=x.outkey set collects=x.c where goods_id=".$outkey;
			$db->query($sql);
		}
		if($type=='video'){
			$sql="update cw_video left join (select count(1) as c,outkey from cw_follow where type='".$type."' and outkey='".$outkey."') x
			on video_id=x.outkey set follow_num=x.c where video_id=".$outkey;
			$db->query($sql);
		}
		if($type=='article'){
			$sql="update cw_article left join (select count(1) as c,outkey from cw_follow where type='".$type."' and outkey='".$outkey."') x
			on article_id=x.outkey set follow_num=x.c where article_id=".$outkey;
			$db->query($sql);
		}
		if($type=='user'){

		}
		$list='取消关注成功';

		$status=1;
	}
	catch (Exception $exp){
		$list=$exp->getMessage();
	}
	return JSONResult($status,$list,$pars);
}


/**
 * 点赞
 * @param unknown_type $pars
 */
function praise($pars){
	$db =&db();
	$status=0;
	$list=array();
	try{
		$outkey=empty($pars->outkey)?0:$pars->outkey;
		$type=empty($pars->type)?'':$pars->type;
		if(empty($pars->uid)){
			throw new Exception('当前人员不存在');
		}

		if(empty($outkey)){
			throw new Exception('点赞失败');
		}
		if(empty($type)){
			throw new Exception('点赞失败');
		}

		$sql="select 1 from cw_praise where type='".$type."' and outkey='".$outkey."' and uid=".$pars->uid;
		$exsits=$db->getOne($sql);
		if($exsits){
			throw new Exception('您已经赞过了');
		}

		$sql="insert into cw_praise set type='".$type."', outkey='".$outkey."',uid=".$pars->uid." ,padddate='".gmtime()."'";
		$db->query($sql);
		if(!$db->affected_rows()){
			throw new Exception('关注失败');
		}
		if($type=='weibo'){
			$sql="update cw_weibo left join (select count(1) as c,outkey from cw_praise where type='".$type."' and outkey='".$outkey."') x
			on wei_id=x.outkey set praisenum=x.c where wei_id=".$outkey;
			$db->query($sql);
		}
		else if($type=='video'){
			$sql="update cw_video left join (select count(1) as c,outkey from cw_praise where type='".$type."' and outkey='".$outkey."') x
			on video_id=x.outkey set praise_num=x.c where video_id=".$outkey;
			$db->query($sql);
		}
		$list='点赞成功';

		$status=1;
	}
	catch (Exception $exp){
		$list=$exp->getMessage();
	}
	return JSONResult($status,$list,$pars);
}

/**
 * 取消点赞
 * @param unknown_type $pars
 */
function unpraise($pars){
	$db =&db();
	$status=0;
	$list=array();
	try{
		$outkey=empty($pars->outkey)?0:$pars->outkey;
		$type=empty($pars->type)?'':$pars->type;
		if(empty($pars->uid)){
			throw new Exception('当前人员不存在');
		}

		if(empty($outkey)){
			throw new Exception('取消点赞失败');
		}
		if(empty($type)){
			throw new Exception('取消点赞失败');
		}

		$sql="select praise_id from cw_praise where type='".$type."' and outkey='".$outkey."' and uid=".$pars->uid;
		$follow_id=$db->getOne($sql);
		if(!$follow_id){
			throw new Exception('您没有赞过');
		}

		$sql="delete from cw_praise where praise_id=".$follow_id;
		$db->query($sql);
		if(!$db->affected_rows()){
			throw new Exception('取消点赞失败');
		}
		if($type=='weibo'){
			$sql="update cw_weibo left join (select count(1) as c,outkey from cw_praise where type='".$type."' and outkey='".$outkey."') x
			on wei_id=x.outkey set praisenum=ifnull(x.c,0) where wei_id=".$outkey;
			$db->query($sql);
		}
		else if($type=='video'){
			$sql="update cw_video left join (select count(1) as c,outkey from cw_praise where type='".$type."' and outkey='".$outkey."') x
			on video_id=x.outkey set praise_num=ifnull(x.c,0) where video_id=".$outkey;
			$db->query($sql);
		}
		$list='取消点赞成功';

		$status=1;
	}
	catch (Exception $exp){
		$list=$exp->getMessage();
	}
	return JSONResult($status,$list,$pars);
}

/**
 * 文章分类
 * @param unknown_type $pars
 * @throws Exception
 */
function articlecategory($pars){
	$db =&db();
	$status=0;
	try{
		$list=array();
		if(empty($pars->uid)){
			throw new Exception('当前人员不存在');
		}
		$sqlw="1=1";
		$type=empty($pars->type)?'':$pars->type;
		if($type){
			$sqlw="cp.cate_name='".$type."'";
		}
		$row=empty($pars->row)?0:$pars->row;
		$size=empty($pars->size)?5:$pars->size;

		$sql="select c.*
		from cw_acategory cp left join cw_acategory c on cp.cate_id=c.parent_id
		where $sqlw order by c.sort_order limit ".$row.",".$size;
		$list=$db->getAll($sql);
		$status=1;
	}
	catch (Exception $exp){
		$list=$exp->getMessage();
	}
	return JSONResult($status,$list,$pars);
}

/**
 * 饲养文章列表
 * @param unknown_type $pars
 * @throws Exception
 */
function articlelist($pars){
	$db =&db();
	$status=0;
	try{
		$list=array();
		if(empty($pars->uid)){
			throw new Exception('当前人员不存在');
		}
		$sqlw="1=1";
		$type=empty($pars->type)?'':$pars->type;
		if($type){
			$sqlw.=" and c.cate_name='".$type."'";
		}
		$title=empty($pars->title)?'':$pars->title;
		if($title){
			$sqlw.=" and (c.cate_name like '%".$title."%' or title like '%".$title."%')";
		}
		$top=empty($pars->top)?0:intval($pars->top);
		if($top==0){
			$sqlw.=" and cat_top=0";
		}
		else if($top==1){
			$sqlw.=" and cat_top=1";
		}
		$row=empty($pars->row)?0:$pars->row;
		$size=empty($pars->size)?5:$pars->size;
		$key=empty($pars->key)?'':$pars->key;
		if($key!=''){
			$sqlw.=" and key like '%".$key."%'";
		}
		$sql="select a.article_id,a.title,a.file_url,CONCAT('".SITE_URL."/index.php?app=article&act=view&article_id=',article_id) as url,add_time,content,follow_id
		from cw_article a 
		LEFT JOIN cw_acategory c on a.cate_id=c.cate_id
		LEFT JOIN cw_follow f on a.article_id=f.outkey and type='article' and f.uid=".$pars->uid." 
		where $sqlw order by add_time desc limit ".$row.",".$size;
		$alist=$db->getAll($sql);
		if($alist){
			foreach ($alist as $k=>$v){
				$v['add_time']=local_date('Y-m-d H:i',$v['add_time']);
				$v['content']=preg_replace("/(\s|\&nbsp\;|　|\xc2\xa0)/", "", strip_tags($v['content']));
				$v['content']=sub_str($v['content'],60);
				$list[]=$v;
			}
		}

		$status=1;
	}
	catch (Exception $exp){
		$list=$exp->getMessage();
	}
	return JSONResult($status,$list,$pars);
}



/**
 * 广场
 * @param unknown_type $pars
 * @throws Exception
 */
function eventlist($pars){
	$db =&db();
	$status=0;
	try{
		$list=array();
		if(empty($pars->uid)){
			throw new Exception('当前人员不存在');
		}
		$sqlw="1=1";
		$type=empty($pars->type)?'':$pars->type;
		if($type){
			$sqlw.=" and c.cate_name='".$type."'";
		}
		$title=empty($pars->title)?'':$pars->title;
		if($title){
			$sqlw.=" and (c.cate_name like '%".$title."%' or title like '%".$title."%')";
		}
		$top=empty($pars->top)?0:intval($pars->top);
		if($top==0){
			$sqlw.=" and cat_top=0";
		}
		else if($top==1){
			$sqlw.=" and cat_top=1";
		}
		$row=empty($pars->row)?0:$pars->row;
		$size=empty($pars->size)?5:$pars->size;
		$key=empty($pars->key)?'':$pars->key;
		if($key!=''){
			$sqlw.=" and key like '%".$key."%'";
		}
		$sql="select a.article_id,a.title,a.file_url,CONCAT('".SITE_URL."/index.php?app=article&act=view&article_id=',article_id) as url,add_time,
			content
			from cw_article a 
			inner JOIN (select * from cw_acategory c 
			where exists(select cw.cate_id from cw_acategory cw where cw.cate_name='广场'
			and c.parent_id= cw.cate_id)) c 
			on a.cate_id=c.cate_id
			where $sqlw order by add_time desc  limit ".$row.",".$size;
		$alist=$db->getAll($sql);
		if($alist){
			foreach ($alist as $k=>$v){
				$v['add_time']=local_date('Y-m-d H:i',$v['add_time']);
				$v['content']=preg_replace("/(\s|\&nbsp\;|　|\xc2\xa0)/", "", strip_tags($v['content']));
				$v['content']=sub_str($v['content'],60);
				$list[]=$v;
			}
		}

		$status=1;
	}
	catch (Exception $exp){
		$list=$exp->getMessage();
	}
	return JSONResult($status,$list,$pars);
}

/**
 * 地区列表
 * @param unknown_type $pars
 * @throws Exception
 */
function regionlist($pars){
	$status=0;
	try{
		$db=&db();
		$list=array();
		if(empty($pars->uid)){
			throw new Exception('当前人员不存在');
		}
		$pid=empty($pars->pid)?0:intval($pars->pid);
		$sql="select region_id,region_name,pinyin from cw_region where parent_id=$pid";

		$list=$db->getAll($sql);

		$status=1;
	}
	catch (Exception $exp){
		$msg=str_replace('\"', '\'', $exp->getMessage());
		$list=$msg;
	}
	return JSONResult($status,$list,$pars);
}

/**
 * 服务机构类型
 * @param unknown_type $pars
 * @throws Exception
 */
function scategory($pars){
	$db =&db();
	$status=0;
	$list=array();
	try{
		if(empty($pars->uid)){
			throw new Exception('当前人员不存在');
		}

		$pid=empty($pars->pid)?0:$pars->pid;

		$sql="select cate_id,cate_name from cw_scategory where parent_id=".$pid." ORDER BY sort_order";
		$list=$db->getAll($sql);

		$status=1;
	}
	catch (Exception $exp){
		$list=$exp->getMessage();
	}
	return JSONResult($status,$list,$pars);
}

/**
 * 服务机构
 * @param unknown_type $pars
 * @throws Exception
 */
function storelist($pars){
	$db =&db();
	$status=0;
	$list=array();
	try{
		if(empty($pars->uid)){
			throw new Exception('当前人员不存在');
		}
		$row=empty($pars->row)?0:$pars->row;
		$size=empty($pars->size)?5:$pars->size;

		$pars->lat=!empty($pars->lat)?floatval($pars->lat):0;
		$pars->lng=!empty($pars->lng)?floatval($pars->lng):0;

		$name=empty($pars->name)?'':$pars->name;
		$region_name=empty($pars->region_name)?'':$pars->region_name;
		$type=empty($pars->type)?'':$pars->type;
		$order=empty($pars->order)?'':$pars->order;

		if("metre"== $order ){
			$orderPart=" metre asc ,star desc ";
		}else if("date"== $order ){
			$orderPart=" add_time asc ";
		}else if("star"== $order ){
			$orderPart=" star desc ";
		}else{
			$orderPart=" metre asc ,star desc ";
		}
		
		$sqlw=" and 1=1";
		if($name){
			$sqlw.=" and store_name like '%".$name."%'";
		}
		if($region_name){
			$sqlw.=" and region_name like '%".$region_name."%'";
		}
		if($type){
			$sqlw.=" and exists(select 1 from cw_category_store cs INNER JOIN cw_scategory c on cs.cate_id=c.cate_id where c.cate_name like '%".$type."%'  and cs.store_id=cc.store_id )";
		}
		/*
		 $sql="select store_id,store_name,store_banner,star,address,
		 round(6378.138*2*asin(sqrt(pow(sin((".$pars->lat."*pi()/180-lat*pi()/180)/2),2)+cos(".$pars->lat."*pi()/180)*cos(lat*pi()/180)* pow(sin((".$pars->lng."*pi()/180-lng*pi()/180)/2),2)))) as metre,store_logo
		 from cw_store where service=1 $sqlw order by sort_order limit $row,$size";	*/
			
		$sql="select store_id,store_name,store_banner,star,address,
round(6378.138*2*asin(sqrt(pow(sin((".$pars->lat."*pi()/180-lat*pi()/180)/2),2)+cos(".$pars->lat."*pi()/180)*cos(lat*pi()/180)* pow(sin((".$pars->lng."*pi()/180-lng*pi()/180)/2),2)))) as metre,store_logo
 from cw_store cc where service=1 $sqlw order by $orderPart limit $row,$size";

		$list=$db->getAll($sql);

		$status=1;
	}
	catch (Exception $exp){
		$list=$exp->getMessage();
	}
	return JSONResult($status,$list,$pars);
}

/**
 * tv类型
 * @param unknown_type $pars
 * @throws Exception
 */
function vcategory($pars){
	$db =&db();
	$status=0;
	$list=array();
	try{
		if(empty($pars->uid)){
			throw new Exception('当前人员不存在');
		}

		$pid=empty($pars->pid)?0:$pars->pid;

		$sql="select cate_id,cate_name from cw_vcategory where isnav=1 ORDER BY sort_order limit 0,5";
		$list=$db->getAll($sql);

		$status=1;
	}
	catch (Exception $exp){
		$list=$exp->getMessage();
	}
	return JSONResult($status,$list,$pars);
}

/**
 * 宠物tv
 * @param unknown_type $pars
 * @throws Exception
 */
function videolist($pars){
	$db =&db();
	$status=0;
	$list=array();
	try{
		if(empty($pars->uid)){
			throw new Exception('当前人员不存在');
		}
		$row=empty($pars->row)?0:$pars->row;
		$size=empty($pars->size)?5:$pars->size;

		$keyword=empty($pars->keyword)?'':$pars->keyword;
		$type=empty($pars->type)?'':$pars->type;

		$sqlw=" checked=1";
		if($keyword){
			$sqlw.=" and content like '%".$keyword."%'";
		}

		if($type){
			$sqlw.=" and cate='".$type."'";
		}

		$sql="select video_id,user_name,real_name,headimgurl,content,hot,rec,pic,file_path,uid,praise_num,cate from cw_video v
LEFT JOIN cw_member m on v.uid=m.user_id where $sqlw order by sort_order limit $row,$size";		
		$list=$db->getAll($sql);

		$status=1;
	}
	catch (Exception $exp){
		$list=$exp->getMessage();
	}
	return JSONResult($status,$list,$pars);
}

/**
 * 活动明细
 * @param unknown_type $pars
 * @throws Exception
 */
function activityinfo($pars){
	$db =&db();
	$status=0;
	$list=array();
	try{
		if(empty($pars->uid)){
			throw new Exception('当前人员不存在');
		}
		$act_id=empty($pars->act_id)?0:$pars->act_id;
		if(empty($act_id)){
			throw new Exception('活动飞走了');
		}
		$sql="select act_id,title,cat,pic,content from cw_activity where act_id=".$act_id;
		$list=$db->getRow($sql);
		if(empty($list)){
			throw new Exception('活动飞走了');
		}

		$status=1;
	}
	catch (Exception $exp){
		$list=$exp->getMessage();
	}
	return JSONResult($status,$list,$pars);
}

/**
 * 微博明细
 * @param unknown_type $pars
 * @throws Exception
 */
function weiboinfo($pars){
	$db =&db();
	$status=0;
	$list=array();
	try{
		if(empty($pars->uid)){
			throw new Exception('当前人员不存在');
		}

		$wei_id=empty($pars->wei_id)?0:$pars->wei_id;
		if(empty($wei_id)){
			throw new Exception('微博飞走了');
		}

		$sql="select wei_id,user_id,user_name,case when ifnull(real_name,'')<>'' then real_name else user_name end as real_name,
		headimgurl,title,w.pic,sharenum,replaynum,follownum,praisenum,w.device,f.follow_id,f1.follow_id as ufollow_id,praise_id,
		pubdate,content,wei_pic,file_path,file_pic,video_id ,
		ifnull(pet.`name`,'') as petname,TIMESTAMPDIFF(year, ifnull(pet.age,curdate()), curdate()) as petage,ifnull(pet.avatar,'') as petavatar,ifnull(pet.cate,'') as petcate ,ifnull(pet.sex,'') as petsex
		from cw_weibo w
		LEFT JOIN cw_follow f on w.wei_id=f.outkey and f.type='weibo' and f.uid=".$pars->uid." 
		LEFT JOIN cw_follow f1 on w.uid=f1.outkey and f1.type='user' and f1.uid=".$pars->uid." 
		LEFT JOIN cw_praise p on w.wei_id=p.outkey and p.type='weibo' and p.uid=".$pars->uid." 
		LEFT JOIN cw_member m on w.uid=m.user_id
		LEFT JOIN cw_pet pet on w.uid=pet.uid and IFNULL(isDefault,0)=1
		where wei_id=".$wei_id;

		$list=$db->getRow($sql);
		if(empty($list)){
			throw new Exception('微博飞走了');
		}

		$list['pubdate']=time_tran(local_date('Y-m-d H:i:s', $list['pubdate']));
		/*if($list['wei_pic']){
			$list['wei_pic']=explode(',', $list['wei_pic']);
			}*/
			
		$replys=array();
		$sql="select reply_id,replydate,content,user_name,case when ifnull(real_name,'')<>'' then real_name else user_name end as real_name,headimgurl,uid from cw_weiboreply r
		LEFT JOIN cw_member m on r.uid=m.user_id where wei_id=".$wei_id." order by replydate desc limit 0,10";
		$replys=$db->getAll($sql);

		foreach ($replys as $k=>$v){
			$v['replydate']=time_tran(local_date('Y-m-d H:i:s', $v['replydate']));
			$list['reply'][]=$v;
		}

		$sql="select case when ifnull(real_name,'')<>'' then real_name else user_name end as real_name,headimgurl,user_id,pubdate
		from cw_weibo w
		inner join cw_member m on w.uid=m.user_id
		where parent_id=".$wei_id." order by pubdate desc";
		$z=$db->getAll($sql);

		if($z){
			foreach ($z as $k=>$v){
				$v['pubdate']=time_tran(local_date('Y-m-d H:i:s', $v['pubdate']));
				$list['zf'][]=$v;
			}
		}

		$sql="select case when ifnull(real_name,'')<>'' then real_name else user_name end as real_name,headimgurl,user_id,fodate
		from cw_follow f
		inner join cw_member m on f.uid=m.user_id 
		where type='weibo' and outkey=".$wei_id." order by fodate desc";
		$z=$db->getAll($sql);
		if($z){
			foreach ($z as $k=>$v){
				$v['fodate']=time_tran(local_date('Y-m-d H:i:s', $v['fodate']));
				$list['sc'][]=$v;
			}
		}

		$sql="select case when ifnull(real_name,'')<>'' then real_name else user_name end as real_name,headimgurl,user_id,padddate
		from cw_praise f
		inner join cw_member m on f.uid=m.user_id 
		where type='weibo' and outkey=".$wei_id." order by padddate desc";
		$z=$db->getAll($sql);
		if($z){
			foreach ($z as $k=>$v){
				$v['padddate']=time_tran(local_date('Y-m-d H:i:s', $v['padddate']));
				$list['z'][]=$v;
			}
		}

		$status=1;
	}
	catch (Exception $exp){
		$list=$exp->getMessage();
	}
	return JSONResult($status,$list,$pars);
}

/**
 * 评论微博
 * @param unknown_type $pars
 * @throws Exception
 */
function replyweibo($pars){
	$db =&db();
	$status=0;
	$list=array();
	try{
		if(empty($pars->uid)){
			throw new Exception('当前人员不存在');
		}

		$weibo_id=empty($pars->weibo_id)?0:$pars->weibo_id;
		$parent_id=empty($pars->parent_id)?0:$pars->parent_id;
		$content=empty($pars->content)?'':$pars->content;

		if(empty($weibo_id)){
			throw new Exception('微博飞走了');
		}

		$sql="select * from cw_weibo where wei_id=".$weibo_id;

		$list=$db->getRow($sql);
		if(empty($list)){
			throw new Exception('微博飞走了');
		}

		if(empty($content)){
			throw new Exception('请您输入内容');
		}

		if($parent_id){
			$sql="select reply_id,uid,m.user_name,m.real_name,m.headimgurl,replydate,content from cw_weiboreply vr
LEFT JOIN cw_member m on vr.uid=m.user_id where reply_id=".$parent_id;
			$parent=$db->getRow($sql);

			if($parent){
				if($parent['real_name']){
					$content.=" // @".$parent['real_name']."：";
				}else{
					$content.=" // @".$parent['user_name']."：";
				}
				$content.=$parent['content'];
			}
		}

		$sql="insert into cw_weiboreply set wei_id=".$weibo_id.",uid=".$pars->uid.",parent_id=".$parent_id.",replydate='".gmtime()."',content='".$content."'";
		$db->query($sql);

		$reply_id=$db->insert_id();
		if(!$reply_id){
			throw new Exception('回复失败');
		}

		$sql="select count(1) from cw_weiboreply where wei_id=".$weibo_id;
		$reply_num=$db->getOne($sql);

		$sql="update cw_weibo set replaynum='".$reply_num."' where wei_id=".$weibo_id;
		$db->query($sql);

		$sql="select reply_id,uid,m.user_name,m.real_name,m.headimgurl,replydate,content from cw_weiboreply vr
LEFT JOIN cw_member m on vr.uid=m.user_id where reply_id=".$reply_id;
		$list=$db->getRow($sql);

		if($list){
			$list['replydate']=time_tran(local_date('Y-m-d H:i:s', $list['replydate']));
		}

		$status=1;
	}
	catch (Exception $exp){
		$list=$exp->getMessage();
	}
	return JSONResult($status,$list,$pars);
}

/**
 * 删除微博评论
 * @param unknown_type $pars
 * @throws Exception
 */
function delweiboreply($pars){
	$db =&db();
	$status=0;
	$list=array();
	try{
		if(empty($pars->uid)){
			throw new Exception('当前人员不存在');
		}

		$reply_id=empty($pars->reply_id)?0:$pars->reply_id;

		if(empty($reply_id)){
			throw new Exception('评论不存在');
		}

		$sql="select wei_id from cw_weiboreply where uid=".$pars->uid." and reply_id=".$reply_id;
		$list=$db->getRow($sql);
		if(empty($list)){
			throw new Exception('评论不存在');
		}

		$sql="delete from cw_weiboreply where uid=".$pars->uid." and reply_id=".$reply_id;
		$db->query($sql);
		if(!$db->affected_rows()){
			throw new Exception('删除评论失败');
		}

		$sql="select count(1) from cw_weiboreply where wei_id=".$list['wei_id'];
		$reply_num=$db->getOne($sql);

		$sql="update cw_weibo set replaynum='".$reply_num."' where wei_id=".$list['wei_id'];
		$db->query($sql);

		$list="删除评论成功";

		$status=1;
	}
	catch (Exception $exp){
		$list=$exp->getMessage();
	}
	return JSONResult($status,$list,$pars);
}

/**
 * 微博评论
 * @param unknown_type $pars
 * @throws Exception
 */
function weiboreplys($pars){
	$db =&db();
	$status=0;
	$list=array();
	try{
		if(empty($pars->uid)){
			throw new Exception('当前人员不存在');
		}
		$row=empty($pars->row)?0:$pars->row;
		$size=empty($pars->size)?5:$pars->size;

		$weibo_id=empty($pars->weibo_id)?0:$pars->weibo_id;
		if(empty($weibo_id)){
			throw new Exception('微博飞走了');
		}

		$sql="select 1 from cw_weibo where wei_id=".$weibo_id;
		$video=$db->getRow($sql);
		if(empty($video)){
			throw new Exception('微博飞走了');
		}

		$sql="select reply_id,uid,m.user_name,m.real_name,m.headimgurl,replydate,content from cw_weiboreply vr
LEFT JOIN cw_member m on vr.uid=m.user_id where wei_id=".$weibo_id." ORDER BY replydate desc limit $row,$size";
		$replys=$db->getAll($sql);

		foreach ($replys as $k=>$v){
			$v['replydate']=time_tran(local_date('Y-m-d H:i:s', $v['replydate']));
			$list[]=$v;
		}

		$status=1;
	}
	catch (Exception $exp){
		$list=$exp->getMessage();
	}
	return JSONResult($status,$list,$pars);
}

/**
 * 删除微博
 * @param unknown_type $pars
 * @throws Exception
 */
function delweibo($pars){
	$db =&db();
	$status=0;
	$list=array();
	try{
		if(empty($pars->uid)){
			throw new Exception('当前人员不存在');
		}

		$wei_id=empty($pars->wei_id)?0:$pars->wei_id;
		if(empty($wei_id)){
			throw new Exception('微博飞走了');
		}

		$sql="select 1 from cw_weibo where uid=".$pars->uid." and wei_id=".$wei_id;

		$list=$db->getRow($sql);
		if(empty($list)){
			throw new Exception('微博飞走了');
		}

		$sql="delete from cw_weibo where uid=".$pars->uid." and wei_id=".$wei_id;

		$db->query($sql);
		if(!$db->affected_rows()){
			throw new Exception('删除微博失败');
		}
			
		$sql="delete from cw_weiboreply where wei_id=".$wei_id;
		$db->query($sql);

		$sql="delete from cw_follow where type='weixin' and outkey=".$wei_id;
		$db->query($sql);

		$list="删除微博成功";
		$status=1;
	}
	catch (Exception $exp){
		$list=$exp->getMessage();
	}
	return JSONResult($status,$list,$pars);
}

/**
 * 发布微博
 * @param unknown_type $pars
 * @throws Exception
 */
function pubweibo($pars){
	$db =&db();
	$status=0;
	$list=array();
	try{
		if(empty($pars->uid)){
			throw new Exception('当前人员不存在');
		}

		$content=empty($pars->content)?'':$pars->content;
		$device=empty($pars->device)?'':$pars->device;
		$wei_pic=empty($pars->wei_pic)?'':$pars->wei_pic;
		$file_path=empty($pars->file_path)?'':$pars->file_path;

		$file_pic=empty($pars->file_pic)?'':$pars->file_pic;

		$video_id=empty($pars->video_id)?0:$pars->video_id;
		if(empty($content)){
			//throw new Exception('请输入要发布的内容');
		}

		if($file_path&&!file_exists(ROOT_PATH."/".$file_path)){
			throw new Exception('视频不存在');
		}

		$pic='';
		if(empty($file_pic)){
			if($file_path&&extension_loaded('ffmpeg')){//判断ffmpeg是否载入
				$mov = new ffmpeg_movie(ROOT_PATH."/".$file_path);//视频的路径
				if($mov){
					$ff_frame = @$mov->getFrame(10);
					if($ff_frame){
						$gd_image = $ff_frame->toGDImage();
						$pic="data/weibov/".md5(time()).".jpg";//要生成图片的绝对路径
						imagejpeg($gd_image, ROOT_PATH."/".$pic);//创建jpg图像
						imagedestroy($gd_image);//销毁一图像
					}
				}
			}}

			if(empty($pic)){
				$pic=$file_pic;
			}
			$sql="insert into cw_weibo set uid='".$pars->uid."',pubdate='".gmtime()."',content='".$content."',
		device='".$device."',wei_pic='".$wei_pic."',file_path='".$file_path."',file_pic='".$pic."',video_id='".$video_id."'";

			$db->query($sql);
			$wei_id=$db->insert_id();
			if(!$wei_id){
				throw new Exception('发布失败');
			}

			if($video_id){
				$sql="update cw_video set share_num=share_num+1 where video_id=".$video_id;
				$db->query($sql);
			}

			$sql="delete from cw_weiboreply where wei_id=".$wei_id;
			$db->query($sql);

			$list=array('wei_id'=>$wei_id);
			$status=1;
	}
	catch (Exception $exp){
		$list=$exp->getMessage();
	}
	return JSONResult($status,$list,$pars);
}

/**
 * 转发微博
 * @param unknown_type $pars
 * @throws Exception
 */
function transweibo($pars){
	$db =&db();
	$status=0;
	$list=array();
	try{
		if(empty($pars->uid)){
			throw new Exception('当前人员不存在');
		}

		$wei_id=empty($pars->wei_id)?'':$pars->wei_id;
		$device=empty($pars->device)?'':$pars->device;
		$content=empty($pars->content)?'':$pars->content;

		if(empty($wei_id)){
			throw new Exception('微博不存在');
		}
		$sql="select * from cw_weibo left join cw_member on uid=user_id where wei_id=$wei_id";
		$weibo = $db->getRow($sql);
		if(empty($weibo)){
			throw new Exception('微博不存在');
		}

		$sql="insert into cw_weibo set uid='".$pars->uid."',pubdate='".gmtime()."',content='".$content.'//[u:'.$weibo['uid'].']'.$weibo['real_name'].'[/u]'.$weibo['content']."',
		device='".$device."',wei_pic='".$weibo['wei_pic']."',file_path='".$weibo['file_path']."',file_pic='".$weibo['file_pic']."',
		video_id='".$weibo['video_id']."',parent_id=".$wei_id;

		$db->query($sql);
		$nwei_id=$db->insert_id();
		if(!$nwei_id){
			throw new Exception('转发失败');
		}
			
		$sql="delete from cw_weiboreply where wei_id=".$nwei_id;
		$db->query($sql);

		$sql="update cw_weibo set sharenum=sharenum+1 where wei_id=".$wei_id;
		$db->query($sql);

		$list=array('wei_id'=>$wei_id);
		$status=1;
	}
	catch (Exception $exp){
		$list=$exp->getMessage();
	}
	return JSONResult($status,$list,$pars);
}

/**
 * 发布视频
 * @param unknown_type $pars
 * @throws Exception
 */
function pubvideo($pars){
	$db =&db();
	$status=0;
	$list=array();
	try{
		if(empty($pars->uid)){
			throw new Exception('当前人员不存在');
		}

		$content=empty($pars->content)?'':$pars->content;
		$file_path=empty($pars->file_path)?'':$pars->file_path;
		$cate=empty($pars->cate)?'':$pars->cate;
		$device=empty($pars->device)?'':$pars->device;

		if(empty($file_path)||!file_exists(ROOT_PATH."/".$file_path)){
			throw new Exception('视频不存在');
		}

		if(empty($content)){
			throw new Exception('请输入要发布的内容');
		}

		if(empty($cate)){
			throw new Exception('请选择视频分类');
		}

		$pic='';
		if(extension_loaded('ffmpeg')){//判断ffmpeg是否载入
			$mov = new ffmpeg_movie(ROOT_PATH."/".$file_path);//视频的路径
			if($mov){
				$ff_frame = $mov->getFrame(10);
				if($ff_frame){
					$gd_image = $ff_frame->toGDImage();
					$pic="data/videocover/".md5(time()).".jpg";//要生成图片的绝对路径
					imagejpeg($gd_image, ROOT_PATH."/".$pic);//创建jpg图像
					imagedestroy($gd_image);//销毁一图像
				}
			}
		}


		$sql="insert into cw_video set uid='".$pars->uid."',pubdate='".gmtime()."',content='".$content."',
		file_path='".$file_path."',pic='".$pic."',cate='".$cate."',device='".$device."'";

		$db->query($sql);
		$video_id=$db->insert_id();
		if(!$video_id){
			throw new Exception('发布失败');
		}

		$list=array('video_id'=>$video_id);
		$status=1;
	}
	catch (Exception $exp){
		$list=$exp->getMessage();
	}
	return JSONResult($status,$list,$pars);
}

/**
 * 删除视频
 * @param unknown_type $pars
 * @throws Exception
 */
function delvideo($pars){
	$db =&db();
	$status=0;
	$list=array();
	try{
		if(empty($pars->uid)){
			throw new Exception('当前人员不存在');
		}

		$video_id=empty($pars->video_id)?0:$pars->video_id;
		if(empty($video_id)){
			throw new Exception('视频飞走了');
		}

		$sql="select file_path,pic from cw_video where uid=".$pars->uid." and video_id=".$video_id;

		$list=$db->getRow($sql);
		if(empty($list)){
			throw new Exception('视频飞走了');
		}

		$sql="delete from cw_video where uid=".$pars->uid." and video_id=".$video_id;

		$db->query($sql);
		if(!$db->affected_rows()){
			throw new Exception('删除视频失败');
		}
		if(file_exists(ROOT_PATH."/".$list['file_path'])){
			@unlink(ROOT_PATH."/".$list['file_path']);
		}
		if(file_exists(ROOT_PATH."/".$list['pic'])){
			@unlink(ROOT_PATH."/".$list['pic']);
		}
			
		$sql="delete from cw_videoreply where video_id=".$video_id;
		$db->query($sql);

		$sql="delete from cw_follow where type='video' and outkey=".$video_id;
		$db->query($sql);

		$list="删除视频成功";
		$status=1;
	}
	catch (Exception $exp){
		$list=$exp->getMessage();
	}
	return JSONResult($status,$list,$pars);
}

/**
 * 视频明细
 * @param unknown_type $pars
 * @throws Exception
 */
function videoinfo($pars){
	$db =&db();
	$status=0;
	$list=array();
	try{
		if(empty($pars->uid)){
			throw new Exception('当前人员不存在');
		}
		$video_id=empty($pars->video_id)?0:$pars->video_id;
		if(empty($video_id)){
			throw new Exception('视频飞走了');
		}

		$sql="select video_id,user_name,real_name,cate,
		headimgurl,content,hot,rec,pic,file_path,v.uid,pubdate,play_num,follow_num,share_num,comment_num,v.device,f.follow_id,f1.follow_id as ufollow_id, praise_num,praise_id
		from  cw_video v
LEFT JOIN cw_member m on v.uid=m.user_id 
LEFT JOIN cw_follow f on v.video_id=f.outkey and f.type='video' and f.uid=".$pars->uid."
LEFT JOIN cw_follow f1 on v.uid=f1.outkey and f1.type='user' and f1.uid=".$pars->uid."  
LEFT JOIN cw_praise p on v.video_id=p.outkey and p.type='video' and p.uid=".$pars->uid." 
where checked=1 and video_id=".$video_id;		
		$list=$db->getRow($sql);
		if(empty($list)){
			throw new Exception('视频飞走了');
		}

		$list['pubdate']=time_tran(local_date('Y-m-d H:i:s', $list['pubdate']));

		$sql="select reply_id,uid,m.user_name,m.real_name,m.headimgurl,replydate,content from cw_videoreply vr
LEFT JOIN cw_member m on vr.uid=m.user_id where video_id=".$video_id." ORDER BY replydate desc limit 0,10";
		$replys=$db->getAll($sql);

		foreach ($replys as $k=>$v){
			$v['replydate']=time_tran(local_date('Y-m-d H:i:s', $v['replydate']));
			$list['reply'][]=$v;
		}
		$sql="select ifnull(video_id,0) as video_id from cw_video where video_id>$video_id and cate='".$list['cate']."' limit 0,1";
		$list['next_vedio_id']=$db->getOne($sql);
		$status=1;
	}
	catch (Exception $exp){
		$list=$exp->getMessage();
	}
	return JSONResult($status,$list,$pars);
}

/**
 * 视频评论列表
 * @param unknown_type $pars
 * @throws Exception
 */
function replyvideolist($pars){
	$db =&db();
	$status=0;
	$list=array();
	try{
		if(empty($pars->uid)){
			throw new Exception('当前人员不存在');
		}

		$video_id=empty($pars->video_id)?0:$pars->video_id;
		$row=empty($pars->row)?0:$pars->row;
		$size=empty($pars->size)?0:$pars->size;

		if(empty($video_id)){
			throw new Exception('视频飞走了');
		}

		$sql="select reply_id,uid,m.user_name,m.real_name,m.headimgurl,replydate,content from cw_videoreply vr
        LEFT JOIN cw_member m on vr.uid=m.user_id where video_id=".$video_id." ORDER BY replydate desc limit $row,$size";
		$replys=$db->getAll($sql);

		foreach ($replys as $k=>$v){
			$v['replydate']=time_tran(local_date('Y-m-d H:i:s', $v['replydate']));
			$list[]=$v;
		}
		$status=1;
	}
	catch (Exception $exp){
		$list=$exp->getMessage();
	}
	return JSONResult($status,$list,$pars);
}







/**
 * 评论视频
 * @param unknown_type $pars
 * @throws Exception
 */
function replyvideo($pars){
	$db =&db();
	$status=0;
	$list=array();
	try{
		if(empty($pars->uid)){
			throw new Exception('当前人员不存在');
		}

		$video_id=empty($pars->video_id)?0:$pars->video_id;
		$parent_id=empty($pars->parent_id)?0:$pars->parent_id;
		$content=empty($pars->content)?'':$pars->content;

		if(empty($video_id)){
			throw new Exception('视频飞走了');
		}

		$sql="select file_path,pic from cw_video where video_id=".$video_id;

		$list=$db->getRow($sql);
		if(empty($list)){
			throw new Exception('视频飞走了');
		}

		if(empty($content)){
			throw new Exception('请您输入内容');
		}

		if($parent_id){
			$sql="select reply_id,uid,m.user_name,m.real_name,m.headimgurl,replydate,content from cw_videoreply vr
LEFT JOIN cw_member m on vr.uid=m.user_id where reply_id=".$parent_id;
			$parent=$db->getRow($sql);

			if($parent){
				if($parent['real_name']){
					$content.=" // @".$parent['real_name']."：";
				}else{
					$content.=" // @".$parent['user_name']."：";
				}
				$content.=$parent['content'];
			}
		}

		$sql="insert into cw_videoreply set video_id=".$video_id.",uid=".$pars->uid.",parent_id=".$parent_id.",replydate='".gmtime()."',content='".$content."'";
		$db->query($sql);

		$reply_id=$db->insert_id();
		if(!$reply_id){
			throw new Exception('回复失败');
		}

		$sql="select count(1) from cw_videoreply where video_id=".$video_id;
		$reply_num=$db->getOne($sql);

		$sql="update cw_video set comment_num='".$reply_num."' where video_id=".$video_id;
		$db->query($sql);

		$sql="select reply_id,uid,m.user_name,m.real_name,m.headimgurl,replydate,content from cw_videoreply vr
LEFT JOIN cw_member m on vr.uid=m.user_id where reply_id=".$reply_id;
		$list=$db->getRow($sql);

		if($list){
			$list['replydate']=time_tran(local_date('Y-m-d H:i:s', $list['replydate']));
		}

		$status=1;
	}
	catch (Exception $exp){
		$list=$exp->getMessage();
	}
	return JSONResult($status,$list,$pars);
}

/**
 * 删除视频评论
 * @param unknown_type $pars
 * @throws Exception
 */
function delvideoreply($pars){
	$db =&db();
	$status=0;
	$list=array();
	try{
		if(empty($pars->uid)){
			throw new Exception('当前人员不存在');
		}

		$reply_id=empty($pars->reply_id)?0:$pars->reply_id;

		if(empty($reply_id)){
			throw new Exception('评论不存在');
		}

		$sql="select video_id from cw_videoreply where uid=".$pars->uid." and reply_id=".$reply_id;
		$list=$db->getRow($sql);
		if(empty($list)){
			throw new Exception('评论不存在');
		}

		$sql="delete from cw_videoreply where uid=".$pars->uid." and reply_id=".$reply_id;
		$db->query($sql);
		if(!$db->affected_rows()){
			throw new Exception('删除评论失败');
		}

		$sql="select count(1) from cw_videoreply where video_id=".$list['video_id'];
		$reply_num=$db->getOne($sql);

		$sql="update cw_video set comment_num='".$reply_num."' where video_id=".$list['video_id'];
		$db->query($sql);

		$list="删除评论成功";

		$status=1;
	}
	catch (Exception $exp){
		$list=$exp->getMessage();
	}
	return JSONResult($status,$list,$pars);
}

/**
 * 视频评论
 * @param unknown_type $pars
 * @throws Exception
 */
function videoreplys($pars){
	$db =&db();
	$status=0;
	$list=array();
	try{
		if(empty($pars->uid)){
			throw new Exception('当前人员不存在');
		}
		$row=empty($pars->row)?0:$pars->row;
		$size=empty($pars->size)?5:$pars->size;

		$video_id=empty($pars->video_id)?0:$pars->video_id;
		if(empty($video_id)){
			throw new Exception('视频飞走了');
		}

		$sql="select 1 from cw_video where checked=1 and video_id=".$video_id;
		$video=$db->getRow($sql);
		if(empty($video)){
			throw new Exception('视频飞走了');
		}

		$sql="select reply_id,uid,m.user_name,m.real_name,m.headimgurl,replydate,content from cw_videoreply vr
LEFT JOIN cw_member m on vr.uid=m.user_id where video_id=".$video_id." ORDER BY replydate desc limit $row,$size";
		$replys=$db->getAll($sql);

		foreach ($replys as $k=>$v){
			$v['replydate']=time_tran(local_date('Y-m-d H:i:s', $v['replydate']));
			$list[]=$v;
		}

		$status=1;
	}
	catch (Exception $exp){
		$list=$exp->getMessage();
	}
	return JSONResult($status,$list,$pars);
}

/**
 * 播放视频
 * @param unknown_type $pars
 * @throws Exception
 */
function videoplay($pars){
	$db =&db();
	$status=0;
	$list=array();
	try{
		if(empty($pars->uid)){
			throw new Exception('当前人员不存在');
		}

		$video_id=empty($pars->video_id)?0:$pars->video_id;

		if(empty($video_id)){
			throw new Exception('视频不存在');
		}

		$sql="update cw_video set play_num=play_num+1 where video_id=".$video_id;
		$db->query($sql);

		$list="";

		$status=1;
	}
	catch (Exception $exp){
		$list=$exp->getMessage();
	}
	return JSONResult($status,$list,$pars);
}

/**
 * 宠物类型
 * @param unknown_type $pars
 * @throws Exception
 */
function pcategory($pars){
	$db =&db();
	$status=0;
	$list=array();
	try{
		$pid=empty($pars->pid)?0:$pars->pid;
		$row=empty($pars->row)?0:$pars->row;
		$size=empty($pars->size)?0:$pars->size;
		$is_common=empty($pars->is_common)?0:$pars->is_common;
		$sql="select cate_id,cate_name,icon from cw_pcategory where if_show=1 and parent_id=$pid and ifnull(is_common,0)=$is_common ORDER BY sort_order";
		if($size>0){
			$sql=$sql." limit $row,$size";
		}
		$list=$db->getAll($sql);
		$status=1;
	}
	catch (Exception $exp){
		$list=$exp->getMessage();
	}
	return JSONResult($status,$list,$pars);
}

/**
 * 添加宠物
 * @param unknown_type $pars
 * @throws Exception
 */
function addpet($pars){
	$db =&db();
	$status=0;
	$list=array();
	try{
		if(empty($pars->uid)){
			throw new Exception('当前人员不存在');
		}

		$name=empty($pars->name)?'':$pars->name;
		$age=empty($pars->age)?'':$pars->age;
		$sex=empty($pars->sex)?'':$pars->sex;
		$cate_id=empty($pars->cate_id)?0:intval($pars->cate_id);
		$pet_id=empty($pars->pet_id)?0:intval($pars->pet_id);

		$pet_avatar=empty($pars->avatar)?'':$pars->avatar; //头像
		$pet_pic=empty($pars->pic)?'':$pars->pic;       //图片
		$pet_barcode=empty($pars->barcode)?'':$pars->barcode;  //二维码


		$pet_status =empty($pars->pet_status)?'':$pars->pet_status;
		$pet_specialty =empty($pars->pet_specialty)?'':$pars->pet_specialty;
		$pet_hate =empty($pars->pet_hate)?'':$pars->pet_hate;
		$pet_like=empty($pars->pet_like)?'':$pars->pet_like;

		if($pet_id){
			$sql="select *,TIMESTAMPDIFF(year, ifnull(age,curdate()), curdate()) as ages from cw_pet where pet_id=$pet_id and uid=".$pars->uid;
			$list=$db->getRow($sql);
			if(empty($list)){
				throw new Exception('宠物不存在');
			}
		}

		$sql="select cate_id,cate_name from cw_pcategory where cate_id=".$cate_id;
		$cate=$db->getRow($sql);
		if(empty($cate)){
			throw new Exception('分类不可用');
		}

		$sql="select 1 from cw_pet where cate_id=".$cate_id." and name='".$name."' and uid=".$pars->uid." and pet_id!=".$pet_id;
		$pet=$db->getRow($sql);
		if($pet){
			throw new Exception('您已经添加了名字为 '.$name.' 的'.$cate['cate_name']);
		}

		if(empty($pet_id)){
			//			$sql="insert into cw_pet set name='".$name."',age='".$age."',
			//		    sex='".$sex."',cate_id='".$cate_id."',cate='".$cate['cate_name']."',add_time='".gmtime()."',uid=".$pars->uid;
			/*cjl start 添加宠物头像 二维码 和图片*/
			$sql="insert into cw_pet set name='".$name."',age='".$age."',
		    sex='".$sex."',cate_id='".$cate_id."',cate='".$cate['cate_name']."',add_time='".gmtime().
		    "',uid=".$pars->uid.",avatar ='".$pet_avatar."',pic ='".$pet_pic."', barcode='".$pet_barcode.
		    "',pet_status='".$pet_status."',pet_specialty='".$pet_specialty.
		    "',pet_hate='".$pet_hate."',pet_like='".$pet_like."'";
			/*cjl  end*/
			$db->query($sql);
			$pet_id=$db->insert_id();
			if(empty($pet_id)){
				throw new Exception('添加宠物失败');
			}
		}else{
			$sql="update cw_pet set name='".$name."',age='".$age."',
			sex='".$sex."',cate_id='".$cate_id."',cate='".$cate['cate_name']."' where pet_id=".$pet_id;
			/*cjl start 添加宠物头像 二维码 和图片*/
			$sql=$sql="update cw_pet set name='".$name."',age='".$age."',sex='".$sex."',cate_id='".$cate_id."',cate='".$cate['cate_name']
			."',avatar ='".$pet_avatar."',pic ='".$pet_pic."', barcode='".$pet_barcode."',pet_status='".$pet_status."',pet_specialty='".$pet_specialty.
            "',pet_hate='".$pet_hate."',pet_like='".$pet_like."' where pet_id=".$pet_id;
			/*cjl end*/
			$db->query($sql);
		}
		$sql="select count(1) from cw_pet where IFNULL(isDefault,0)=1 and uid=".$pars->uid;
		$count=$db->getOne($sql);
		if($count<1){
			$sql="update cw_pet set isDefault =1 where pet_id=".$pet_id;
			$db->query($sql);
		}

		$list="操作成功";
		$status=1;
	}
	catch (Exception $exp){
		$list=$exp->getMessage();
	}
	return JSONResult($status,$list,$pars);
}
/*cjl start */

/**
 * 设置默认值
 * @param unknown_type $pars
 * @throws Exception
 * @return string
 */
function petdefault($pars){
	$db =&db();
	$status=0;
	$list=array();
	try{
		if(empty($pars->uid)){
			throw new Exception('当前人员不存在');
		}
		$pet_id=empty($pars->pet_id)?0:intval($pars->pet_id);
		if($pet_id){
			$sql="select *,TIMESTAMPDIFF(year, ifnull(age,curdate()), curdate()) as age from cw_pet where pet_id=$pet_id and uid=".$pars->uid;
			$list=$db->getRow($sql);
			if(empty($list)){
				throw new Exception('宠物不存在');
			}
		}
		$sql="update cw_pet set isDefault=0 where uid=".$pars->uid;
		$db->query($sql);
		$sql="update cw_pet set isDefault=1 where pet_id=$pet_id and uid=".$pars->uid;
		$db->query($sql);
		$list="操作成功";
		$status=1;
	}
	catch (Exception $exp){
		$list=$exp->getMessage();
	}
	return JSONResult($status,$list,$pars);
}
/*cjl end*/

/**
 * 宠物年龄
 * @param unknown_type $pars
 */
function petage($pars){
	$db =&db();
	$status=0;
	try{
		$sql="select d_key,d_value from cw_dictionary where d_code='petage' order by d_order";
		$list=$db->getAll($sql);
		$status=1;
	}
	catch (Exception $exp){
		$list=$exp->getMessage();
	}
	return JSONResult($status,$list,$pars);
}
function petspecialty($pars){
	$db =&db();
	$status=0;
	try{
		$sql="select d_key,d_value from cw_dictionary where d_code='petspecialty' order by d_order";
		$list=$db->getAll($sql);
		$status=1;
	}
	catch (Exception $exp){
		$list=$exp->getMessage();
	}
	return JSONResult($status,$list,$pars);
}
function pethate($pars){
	$db =&db();
	$status=0;
	try{
		$sql="select d_key,d_value from cw_dictionary where d_code='pethate' order by d_order";
		$list=$db->getAll($sql);
		$status=1;
	}
	catch (Exception $exp){
		$list=$exp->getMessage();
	}
	return JSONResult($status,$list,$pars);
}
function petlike($pars){
	$db =&db();
	$status=0;
	try{
		$sql="select d_key,d_value from cw_dictionary where d_code='petlike' order by d_order";
		$list=$db->getAll($sql);
		$status=1;
	}
	catch (Exception $exp){
		$list=$exp->getMessage();
	}
	return JSONResult($status,$list,$pars);
}
function petstatus($pars){
	$db =&db();
	$status=0;
	try{
		$sql="select d_key,d_value from cw_dictionary where d_code='petstatus' order by d_order";
		$list=$db->getAll($sql);
		$status=1;
	}
	catch (Exception $exp){
		$list=$exp->getMessage();
	}
	return JSONResult($status,$list,$pars);
}

/**
 * 宠物列表
 * @param unknown_type $pars
 * @throws Exception
 */
function pets($pars){
	$db =&db();
	$status=0;
	$list=array();
	try{
		if(empty($pars->uid)){
			throw new Exception('当前人员不存在');
		}

		$view_uid=empty($pars->view_uid)?0:intval($pars->view_uid);
		if($view_uid){
			$pars->uid=$view_uid;
		}

		$sql="select *,TIMESTAMPDIFF(year, ifnull(age,curdate()), curdate()) as ages   from cw_pet where uid=".$pars->uid;
		$pet=$db->getAll($sql);
		if($pet){
			foreach ($pet as $k=>$v){
				$v['add_time']=local_date('Y-m-d H:i',$v['add_time']);
				$list[]=$v;
			}
		}

		$status=1;
	}
	catch (Exception $exp){
		$list=$exp->getMessage();
	}
	return JSONResult($status,$list,$pars);
}

/**
 * 宠物明细
 * @param unknown_type $pars
 * @throws Exception
 */
function petinfo($pars){
	$db =&db();
	$status=0;
	$list=array();
	try{
		if(empty($pars->uid)){
			throw new Exception('当前人员不存在');
		}

		$view_uid=empty($pars->view_uid)?0:intval($pars->view_uid);
		if($view_uid){
			$pars->uid=$view_uid;
		}

		$pet_id=empty($pars->pet_id)?0:intval($pars->pet_id);
		if(empty($pet_id)){
			throw new Exception('宠物不存在');
		}
		$sql="select *,TIMESTAMPDIFF(year, ifnull(age,curdate()), curdate())as ages from cw_pet where pet_id=$pet_id";
		$list=$db->getRow($sql);
		if(empty($list)){
			throw new Exception('宠物不存在');
		}
		$list['add_time']=local_date('Y-m-d H:i',$list['add_time']);

		$status=1;
	}
	catch (Exception $exp){
		$list=$exp->getMessage();
	}
	return JSONResult($status,$list,$pars);
}

/**
 * 删除宠物
 * @param unknown_type $pars
 * @throws Exception
 */
function delpet($pars){
	$db =&db();
	$status=0;
	$list=array();
	try{
		if(empty($pars->uid)){
			throw new Exception('当前人员不存在');
		}

		$pet_id=empty($pars->pet_id)?0:intval($pars->pet_id);
		if(empty($pet_id)){
			throw new Exception('宠物不存在');
		}
		$sql="select 1 from cw_pet where pet_id=$pet_id and uid=".$pars->uid;
		$list=$db->getRow($sql);
		if(empty($list)){
			throw new Exception('宠物不存在');
		}
		$sql="delete from cw_pet where pet_id=$pet_id and uid=".$pars->uid;
		$db->query($sql);
		$r=$db->affected_rows();
		if(empty($r)){
			throw new Exception('宠物删除失败');
		}
		$list="宠物删除成功";

		$sql="select count(1) from cw_pet where uid=".$pars->uid;
		$count=$db->getOne($sql);
		if($count=1)
		$sql="update cw_pet set isDefault =1 where uid=".$pars->uid;

		$status=1;
	}
	catch (Exception $exp){
		$list=$exp->getMessage();
	}
	return JSONResult($status,$list,$pars);
}

/**
 * 我的关注
 * @param unknown_type $pars
 * @throws Exception
 */
function follows($pars){
	$db =&db();
	$status=0;
	$list=array();
	try{
		if(empty($pars->uid)){
			throw new Exception('当前人员不存在');
		}

		$type=empty($pars->type)?'':$pars->type;
		if(empty($type)){
			throw new Exception('参数错误');
		}
		$row=empty($pars->row)?0:$pars->row;
		$size=empty($pars->size)?5:$pars->size;

		if($type=='goods'){

			$sql="select fodate,g.goods_id,goods_name,price,default_image,old_price,noshipfee,s.store_name,g.store_id,gs.sales,tags,brand from cw_follow f
INNER JOIN cw_goods g on f.outkey=g.goods_id
LEFT JOIN cw_store s on g.store_id=s.store_id
LEFT JOIN cw_goods_statistics gs on g.goods_id=gs.goods_id
 where uid=".$pars->uid." and f.type='goods' and g.if_show=1 and closed=0 order by fodate desc limit $row,$size";

		}elseif($type=='store'){

			$sql="select fodate,state,close_reason,store_id,store_name,owner_name,region_name,address,tel,store_banner,store_logo,description,star,praise_rate from cw_follow f
INNER JOIN cw_store s on f.outkey=s.store_id
 where uid=".$pars->uid." and f.type='store' and s.state=1 order by fodate desc limit $row,$size";

		}elseif($type=='weibo'){

			$sql="select fodate,wei_id,user_id,user_name,real_name,headimgurl,title,pic,sharenum,replaynum,follownum,w.device,pubdate from cw_follow f
INNER JOIN cw_weibo w on f.outkey=w.wei_id
LEFT JOIN cw_member m on w.uid=m.user_id
 where f.uid=".$pars->uid." and f.type='weibo' order by fodate desc limit $row,$size";

		}elseif($type=='video'){

			$sql="select fodate,video_id,user_name,real_name,
		headimgurl,content,hot,rec,pic,file_path,v.uid,pubdate,play_num,follow_num,praise_num,share_num,comment_num,v.device,cate from cw_follow f
INNER JOIN cw_video v on f.outkey=v.video_id
LEFT JOIN cw_member m on v.uid=m.user_id
 where f.uid=".$pars->uid." and f.type='video' and v.checked=1 order by fodate desc limit $row,$size";

		}elseif($type=='article'){
			$sql="select a.article_id,a.title,a.file_url,CONCAT('".SITE_URL."/index.php?app=article&act=view&article_id=',article_id) as url,add_time,content,follow_id
		from cw_follow f 
		LEFT JOIN cw_article a on a.article_id=f.outkey
		LEFT JOIN cw_acategory c on a.cate_id=c.cate_id
		where type='article' and f.uid=".$pars->uid." 
		order by add_time desc limit ".$row.",".$size;
		}
		elseif($type=='user'){
			$sql="select user_id,user_name,real_name,headimgurl,fodate,follow_id from cw_follow f
		LEFT JOIN cw_member m on m.user_id=f.outkey
		where type='user' and f.uid=".$pars->uid." 
		order by fodate desc limit ".$row.",".$size;
		}else{
			throw new Exception('参数错误');
		}
		$f=$db->getAll($sql);
		if($f){
			foreach ($f as $k=>$v){
				$v['fodate']=local_date('Y-m-d H:i',$v['fodate']);
				if(!empty($v['pubdate'])){
					$v['pubdate']=local_date('Y-m-d H:i',$v['pubdate']);
				}
				if(!empty($v['add_time'])){
					$v['add_time']=local_date('Y-m-d H:i',$v['add_time']);
				}
				if(!empty($v['content'])){
					$v['content']=strip_tags($v['content']);
					$v['content']=sub_str($v['content'],60);
				}
				$list[]=$v;
			}

		}

		$status=1;
	}
	catch (Exception $exp){
		$list=$exp->getMessage();
	}
	return JSONResult($status,$list,$pars);
}

?>